package com.ddpt.platform.common.base;

import com.ddpt.platform.common.exception.ErrorEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * 响应数据
 * @param <T>
 */
@Data
public class ResponseEntity<T> extends BaseEntity {

    private static final int SUCCESS = 200;

    private static final int FAIL = 500;

    /** 响应码  200代表成功*/
    private int code = SUCCESS;

    /** 响应数据*/
    private T payload;

    /** 分页信息*/
    private CommonPage page;

    /** 错误消息*/
    private String errorMsg;

    /** 错误详细信息*/
    private String errorMsgDesc;

    public static <T> ResponseEntity<T> error(String remark){
        return error(null,remark);
    }

    public static <T> ResponseEntity<T> error(ErrorEnum ee, String remark){
        if(ee == null){
            ee = ErrorEnum.error;
        }
        ResponseEntity<T> entity = new ResponseEntity<>();
        entity.setCode(FAIL);
        entity.setErrorMsg(StringUtils.isNotBlank(remark)?remark:ee.getRemark());
        entity.setErrorMsgDesc(entity.getErrorMsg());
        return entity;
    }

    public static <T> ResponseEntity<T> success(){
        return success(null);
    }

    public static <T> ResponseEntity<T> success(T payload){
        ResponseEntity<T> entity = new ResponseEntity<>();
        entity.setPayload(payload);
        return entity;
    }

    public boolean isSuccess(){
        return this.getCode() == SUCCESS;
    }
}
